package com.dtsz.m2.ui.activity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.Request;
import com.dtsz.m2.R;
import com.dtsz.m2.config.UrlConfig;
import com.dtsz.m2.ui.base.BaseActivity;
import com.dtsz.m2.ui.inter.OnPasswordInputFinish;
import com.dtsz.m2.ui.model.PayOrderVo;
import com.dtsz.m2.ui.model.Users;
import com.dtsz.m2.ui.widget.PopEnterPassword;
import com.dtsz.m2.ui.widget.PopEnterResetPassword;
import com.dtsz.m2.utils.RSAEncrypt;
import com.dtsz.m2.utils.StringUtils;
import com.dtsz.m2.volley.state.HUCState;

public class Pay2Activity extends BaseActivity implements OnPasswordInputFinish {
    private TextView tvName;
    private EditText etNum, etRemake;
    private PayOrderVo payOrderVo = new PayOrderVo();
    private PopEnterPassword popEnterPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_pay2);
        super.onCreate(savedInstanceState);
        tvActionbarTitle.setText("转出数量");
        String address = getIntent().getExtras().getString("address");
        String name = getIntent().getExtras().getString("name");
        tvName.setText(name);
        payOrderVo.setAddress(address);
    }

    @Override
    public void initView() {
        super.initView();
        tvName = findViewById(R.id.tv_name);
        etNum = findViewById(R.id.et_num);
        etRemake = findViewById(R.id.et_remake);
    }

    @Override
    public void initClick() {
        super.initClick();
        findViewById(R.id.bt_pwd).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.bt_pwd:
                String num = etNum.getText().toString();
                if (StringUtils.isEmpty(num)) {
                    showToast("数量不能为空");
                    return;
                }
                String remake = etRemake.getText().toString();
                if (!StringUtils.isEmpty(remake)) {
                    payOrderVo.setRemake(remake);
                }
                payOrderVo.setCount(num);
                initNetWork();
                break;
        }
    }

    private void initNetWork() {
        loading();
        payOrderVo.setToken(pageVo.getToken());
        hucRequestInterfase.executeStr(this, String.class,
                HUCState.HUC_REQEUST_CODE_1, Request.Method.POST, UrlConfig.ADVANCEINWARDTURN_URL,
                hucParameInterfase.getRequestParameter(payOrderVo), true);
    }

    @Override
    public void onSuccessfulStrType(int inputType, Object response) {
        super.onSuccessfulStrType(inputType, response);
        switch (inputType) {
            case HUCState.HUC_REQEUST_CODE_1:
                String str = (String) response;
                if (str.indexOf("M2") == -1) {
                    showToast(str);
                } else {
                    pageVo.setId(str);
                    // sendIntent(this, NormalKeyBoardActivity.class);
                    if (popEnterPassword == null) {
                        popEnterPassword = new PopEnterPassword(this, this, etNum.getText().toString() + "㎡");
                        // 显示窗口
                        popEnterPassword.showAtLocation(this.findViewById(R.id.bt_pwd),
                                Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); // 设置layout在PopupWindow中显示的位置
                    }
                }
                break;
            case HUCState.HUC_REQEUST_CODE_2:
                String st1r = (String) response;
                if ("转出成功".equals(st1r)) {
                    if (popEnterPassword != null) {
                        popEnterPassword.dismiss();
                        popEnterPassword = null;
                    }
                    finish();
                }
                showToast(st1r);
                break;
        }
        loadDismiss();
    }

    @Override
    public void inputFinish(String password) {
        if (StringUtils.isEmpty(password)) {
            if (popEnterPassword != null) {
                popEnterPassword.dismiss();
                popEnterPassword = null;
            }
            return;
        }
        loading();
        pageVo.setRsa(RSAEncrypt.encrypt(password));
        hucRequestInterfase.executeStr(this, Users.class,
                HUCState.HUC_REQEUST_CODE_2, Request.Method.POST, UrlConfig.INWARDTURN_URL,
                hucParameInterfase.getRequestParameter(pageVo), true);

    }
}
